<?php
/*
 * 首页
 */
namespace app\home\controller;

use app\common\controller\IndexBase;
use think\facade\Session;
use think\facade\Db;
use app\common\model\Wechat;

class Index extends IndexBase
{

    protected function initialize()
    {
        parent::initialize();
        $this->app = Wechat::wechat();
    }
    //首页
    public function index()
    {

        $ip = $this->request->ip();
        $data ['city'] = '本地环境';
        if ($ip != '127.0.0.1')
        {
            $url = "https://apis.map.qq.com/ws/location/v1/ip?ip=".$ip."&key=TKUBZ-D24AF-GJ4JY-JDVM2-IBYKK-KEBCU";//腾讯IP定位
            $json = https_request($url);
            $res = json_to_array($json);
            $res['status'] && $data['city'] = $res['message'];
            !$res['status'] && $data['city'] = $res['result']['ad_info']['nation'].$res['result']['ad_info']['province'].$res['result']['ad_info']['city'].$res['result']['ad_info']['district'];
	        $rs = Db::table('see_log')->where('ip',$ip)->whereTime('create_time','today')->find();
            if(!$this->request->isMobile()){
                $agent = $this->request->header('user-agent');
            }
	        if(!$rs){
	            Db::table('see_log')->insert([
	                'ip' => $ip,
	                'city' => $data['city'],
                    'agent' => $agent??'',
	                'create_time'=>time(),
	                'update_time'=>time(),
	                'count'=>1
	            ]);
	        }
        }
        $data['banner'] = Db::table('advert')->with('adcat')->where('ad_cat_id',1)->select();
        $data['goods'] = Db::table('goods')->where(['status'=>1])->order('id','DESC')->select()->toArray();
        foreach ($data['goods'] as $key => $v) {
            $data['goods'][$key]['id'] = $this->hashids->encode($v['id']);
            $data['goods'][$key]['img'] = json_decode($v['img']);
        }
        $data['menu']   = Db::table('menu')->field('id,name,icon img,url')->where(['delete_time'=>0,'status'=>1,'type'=>0])->order('sort','ASC')->select()->toArray();

        foreach ($data['menu'] as $key => $v) {
            $data['menu'][$key]['id'] = $this->hashids->encode($v['id']);
        }
        $server = [
            [
                'name'=>'充值',
                'desc'=>'返现+升会员',
                'img'=>'https://www.zztuku.com/demo/9546/images/item-001.png',
                'url'=>''
            ],[
                'name'=>'领券',
                'desc'=>'返现+升会员',
                'img'=>'https://www.zztuku.com/demo/9546/images/item-002.png',
                'url'=>''
            ],[
                'name'=>'分享',
                'desc'=>'返现+升会员',
                'img'=>'https://www.zztuku.com/demo/9546/images/item-003.png',
                'url'=>''
            ],[
                'name'=>'抽奖',
                'desc'=>'返现+升会员',
                'img'=>'https://www.zztuku.com/demo/9546/images/item-004.png',
                'url'=>''
            ]
        ];
        $data['server'] = $server;
        $cate_goods = $this->get_index_cat_goods();
        return $this->fetch('index',['data'=>$data,'cate_goods'=>$cate_goods]);
    }
    //定位地址
    public function addr()
    {
        if ($this->request->isPost())
        {
        	$param = $this->request->param();
        	$url = 'https://apis.map.qq.com/ws/geocoder/v1/?location='.$param['lat'].','.$param['lon'].'&key=VAZBZ-QONR2-2MOUP-CMOEX-RRVWT-3XFZZ&get_poi=1';
        	$json = https_request($url);
        	$res = json_to_array($json);
        	$city = $res['result']['formatted_addresses']['recommend']?$res['result']['formatted_addresses']['recommend']:$res['result']['address'];
        	$ip = $this->request->ip();
        	if ($ip != '127.0.0.1')
	        {
				$rs = Db::table('see_log')->where('ip',$ip)->whereTime('create_time','today')->find();
		        if($rs){
            		Db::table('see_log')->where('id',$rs['id'])->inc('count')->update(['address'=>$city]);//记录日志
		        }else{
		            Db::table('see_log')->insert([
		                'ip' => $ip,
		                'address' => $city,
		                'create_time'=>time(),
		                'update_time'=>time(),
		                'count'=>1
		            ]);
		        }
		    }
		    $this->success($city);
        }
        $this->error('页面被火星人抓走了～');
    }
    //消息通知
    public function notice()
    {
        if ($this->request->isPost())
        {
            $res = Db::table('notice')->field('name,content,sort,create_time')->where('status',1)->order('sort','DESC')->find();
            empty($res) && $this->error('无消息！');
            $this->success('查询成功！','',$res);
        }
        $this->error('页面被火星人抓走了～');
    }

    //查询首页显示分类及分类下推荐商品
    protected function get_index_cat_goods()
    {
        $data = Db::table('good_cate')->field('id,name,img')->where(['index'=>1])->order('sort','DESC')->select()->toArray();//根据排序获取首页显示分类
        foreach ($data as $key => $v)
        {
            $data[$key]['id'] = Db::table('goods')->where(['status'=>1,'is_best'=>1,'cate_id'=>$v['id']])->order('sort','DESC')->limit(2)->select()->toArray();
            foreach ($data[$key]['id'] as $k => $vv)
            {
                $data[$key]['id'][$k]['id'] = $this->hashids->encode($vv['id']);
                $data[$key]['id'][$k]['img'] = json_decode($vv['img']);
            }
            
        }
        return $data;
    }

    //微信配置
    public function weconfig()
    {
        if ($this->request->isPost())
        {
            if (in_wechat()) {
                // $url = 'https://qcwd.com/city.html';
                $param = $this->request->param('curl');
                $url = 'https://qcwd.com/'.trim($param).'.html';
                $this->app->jssdk->setUrl($url);
                $info = $this->app->jssdk->buildConfig(['checkJsApi','getLocation','openLocation','scanQRCode']);//微信扫一扫配置
                return $info;
            }
        }
        $this->error('页面被火星人抓走了～');
    }

}